<?php

App::import(array('Sanitize'));

class PostsController extends AppController {
    
    var $actsAs = array('Containable');
    
    var $name = 'Posts';
    
    var $uses = array('Category','Post','District','Media');
    
    var $components = array('Email');        
    
    
    function index() {
        
        $homebanners=$this->Media->find("all",array("recursive"=>-1,"conditions"=>array("type"=>"homebanner")));
        
        
        
        $this->set(compact("homebanners"));
        
        
    }
    
    // search
    
    function search($category=null) {
        
         extract($this->params['named']);
         extract($this->params['url']);
         
         
         
         $options=array(
            'conditions'=>array('Post.status'=>'published'),
            'limit'=>15
            );
         
         if(isset($category)){
             //$categories=explode(',',$categories);
             $options['conditions']['category_id']=$category;
             
             $item_cat=$this->Category->find('first',
                array(
                    'recursive'=>-1,
                    'conditions'=>array('id'=>$category)
                )
             );
             
             $this->set(compact("item_cat"));
             
         }
         else $category=array();
         
         $this->set(compact("category"));
         
         if(isset($location)){
             $options['conditions']['District.id']=$location;
             $item_location=$this->District->find('first',
                array(
                    'recursive'=>-1,
                    'conditions'=>array('District.id'=>$location)
                )
             );
             
             if($item_location["District"]["parent_id"]){
                $item_location["District"]["Parent"]=$this->District->find('first',
                    array(
                        'recursive'=>-1,
                        'conditions'=>array('District.id'=>$item_location["District"]["parent_id"])
                    )
                 );
                     
             }
             
             
             $this->set(compact("item_location"));
         }
         else $location="";
         
         $this->set(compact("location"));
         
         
         if(isset($look)){
             $options['conditions']['or']['Category.name like ']="%$look%";
             $options['conditions']['or']['Post.name like ']="%$look%";
             $options['conditions']['or']['Post.bangla_name like ']="%$look%";
             $options['conditions']['or']['Post.address like ']="%$look%";
             $options['conditions']['or']['Post.contact_address like ']="%$look%";
             $options['conditions']['or']['Post.additional like ']="%$look%";
             $options['conditions']['or']['Post.bangla_additional like ']="%$look%";
             $options['conditions']['or']['Post.bangla_contact_address like ']="%$look%";
             $options['conditions']['or']['Post.bangla_address like ']="%$look%";
             $options['conditions']['or']['Post.phone like ']="%$look%";
         }
         else $look='';
         
         $options['recursive']=1;
         
         $this->paginate['Post']=$options;
         
         $posts=$this->paginate('Post');
         
         $tmp_posts=array();
         foreach($posts as $key=>$item){
            $tmp_posts[$key]=$item;
            $media=$this->Media->find('first',array('conditions'=>array('foreign_key_id'=>($item['Post']['user_id']?$item['Post']['user_id']:$item['Post']['id']))));
            $tmp_posts[$key]=am($tmp_posts[$key],$media);
         }
         
         $posts=$tmp_posts;
         
         //pr($posts);
         
         $this->set(compact('posts'));
         
         
         $cats=$this->Category->find('all',
            array(
                'recursive'=>-1,
                'order'=>'Category.name'
            )
         );
         $locals=$this->District->find('threaded',
            array(
                'recursive'=>-1,
                
            )
         );
         
         $this->set(compact('locals','cats','categories','locations','look'));
         
         $this->set('noleftbar',true);
         
         
             
    }
    
    
    
    // details Post
    
    function details($id=null) {
        if($id==null){
            $this->redirect($this->referer());
            exit;
        }
        
        $options=array(
                'Post.id'=>$id,
        );
        
        
        /*if(($details = Cache::read('details_'.$id, 'default')) === false) :
        
        */    
        
        $this->Post->Category->unBindModel(array('hasMany' => array('Post')));
        $this->Post->District->unBindModel(array('hasMany' => array('Post')));
            
        $details=$this->Post->find('first',array('conditions'=>$options,'recursive'=>2));
        

        if($details["District"]["parent_id"])        
           $details["District"]["Parent"]=$this->District->find('first',array('conditions'=>array("id"=>$details["District"]["parent_id"]),'recursive'=>-1));

        /*    Cache::write('details_'.$id, $details, 'default');
        endif;*/
        
        $details["Media"]=$this->Media->find('all',array('conditions'=>array('foreign_key_id'=>($details['Post']['user_id']?$details['Post']['user_id']:$details['Post']['id']))));
        
        $this->set('details',$details);
        
        $this->Post->updateAll(array('view_count'=>$details['Post']['view_count']+1),$options);
        
        $this->front_page_title=$details['Post']['name'];         
    }
    
    
    function __getCatList() {
        
        $options = array(
            'recursive' => -1,
            'fields' => array('id','name','parent_id'),
            'order' => array('lft' => 'ASC'),
        );
        
        
        
        $tmp = $this->Category->find('all', $options);
        $categories=array();
        foreach($tmp as $cat){
            if($cat['Category']['id']!=1)
                $categories[$cat['Category']['id']]=$cat['Category']['name'];
        }
        
    
        return $categories;
    }
    
    

   

    
    function __clean($content) {
        $content = strip_tags($content);
        $content = preg_replace('/ style.*?=.*?(".*?"|\'.*?\')/', '', $content);
        $content = trim ($content);
        $content = $this->restoreTags($content);
        return $content;
    }
    
    
    
    // Original PHP code by Chirp Internet: www.chirp.com.au
    protected function restoreTags($input) {
        $opened = array();

        // loop through opened and closed tags in order
        if(preg_match_all("/<(\/?[a-z]+)>?/i", $input, $matches)) {
            foreach($matches[1] as $tag) {
                if(preg_match("/^[a-z]+$/i", $tag, $regs)) {
                    // a tag has been opened
                    if(strtolower($regs[0]) != 'br') $opened[] = $regs[0];
                } elseif(preg_match("/^\/([a-z]+)$/i", $tag, $regs)) {
                    // a tag has been closed
                    unset($opened[array_pop(array_keys($opened, $regs[1]))]);
                }
            }
        }

        // close tags that are still open
        if($opened) {
        $tagstoclose = array_reverse($opened);
        foreach($tagstoclose as $tag) $input .= "</$tag>";
    }

    return $input;
    }
    
    
    
    
    
    
    
    function clean($name = 'Post') {
        Cache::clear(false, $name);
        $this->redirect('/');
    }
    
    
    
        
    
   
    
}
?>
